package com.xy.io;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;

public abstract class AbstractCodeMap<K, V> implements Serializable {
    protected int code;
    protected Map<K, Object> data;

    public AbstractCodeMap() {
        this.data = new HashMap<K, Object>();
    }

    public abstract void toCode(Object key);

    public void clear() {
        this.data.clear();
    }

    public boolean haveKey(K key) {
        if (this.data.containsKey(key)) {
            return true;
        } else {
            return false;
        }
    }

    public void put(K key, V value) {
        this.data.put(key, value);
    }

    public JSONObject getJSONObjectByMap(Object maps) {
        try {
            String a = JSONObject.toJSONString(maps);
            return JSONObject.parseObject(a);
        } catch (Exception e) {
            return null;
        }
        //return monyes.toString();
    }


    public Object get(Object key) {
        return this.data.get(key);
    }

    public Map<K, Object> getData() {
        return data;
    }

    public void setData(Map<K, V> data) {
        for (Map.Entry<K, V> go : data.entrySet()) {
            put(go.getKey(), go.getValue());
        }
    }

    public int getCode() {
        return code;
    }

    public AbstractCodeMap<K, V> setCode(int code) {
        this.code = code;
        return this;
    }

}
